<template>
    <div>
      <div style="margin: 10px 0">
        <el-input style="width: 200px" placeholder="请输入标题" suffix-icon="el-icon-search" v-model="title"></el-input>
        <el-input style="width: 200px" placeholder="请输入内容" suffix-icon="el-icon-message" class="ml-5" v-model="content"></el-input>
        <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </div>
  
      <div style="margin: 10px 0" >
        <el-button type="primary" @click="handleAdd" >新增 <i class="el-icon-circle-plus-outline"></i></el-button>
        <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='我再想想'
            icon="el-icon-info"
            icon-color="red"
            title="您确定批量删除这些数据吗？"
            @confirm="delBatch"
        >
          <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </div>
  
      <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="title" label="标题" width="180"></el-table-column>
        <el-table-column prop="content" label="内容" width="300"></el-table-column>
        <el-table-column prop="attachment" label="附件" width="360"></el-table-column>
        <el-table-column prop="time" label="发布时间"></el-table-column>
        <el-table-column label="操作"  width="200" align="center">
          <template slot-scope="scope">
            <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="del(scope.row.id)"
            >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding: 10px 0">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
  
      <el-dialog title="公告信息" :visible.sync="dialogFormVisible" width="30%" >
        <el-form label-width="80px" size="small">
          <el-form-item label="标题">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="内容">
            <el-input v-model="form.content" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="附件上传">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
           multiple
           accept=".jpg,.png,.doc,.docx,.xlsx,.xls,.pptx,.txt"
          :data="{ fileName: fileName}"
          :on-success="handleSuccess"
          :show-file-list="true"
          :before-upload="beforeAvatarUpload"
        >
          <el-button type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png/doc/pdf/excel/txt文件，且不超过2MB
          </div>
        </el-upload>
      </el-form-item>

          <el-form-item label="发布时间">
            <el-input v-model="form.time" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">发布</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  export default {
    name: "Notice",
    data() {
      return {
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 10,
        title: "",
        content: "",
        form: {},
        dialogFormVisible: false,
        multipleSelection: [],

        fileName: '',
        attachmentUrl: [],
        uploadUrl: 'http://localhost:8086/upload' // 上传文件的接口地址
      }
    },
    created() {
      this.load()
    },
    methods: {

     

      load() {
        this.request.get("/notice/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            title: this.title,
            content: this.content,
          }
        }).then(res => {
          const data = res.data
          console.log(data.data)
          this.tableData = data.data.list
          this.total = data.data.total
  
  
        })
      },
      save() {
        this.request.post("/notice/insert", 
        {
          id: this.form.id,
          title: this.form.title,
          content: this.form.content,
          attachment: this.attachmentUrl,
          time: this.form.time,
        }).then(res => {
          if (res.data.code === 200) {
            this.$message.success(" 发布成功 ")
            this.dialogFormVisible = false
            this.load()
          } else {
            this.$message.error(" 发布失败 ")
          }
        })
      },
      handleAdd() {
        this.dialogFormVisible = true
        this.form = {}
      },
      handleEdit(row) {
        this.form = row
        this.dialogFormVisible = true
      },
      del(id) {
        this.request.delete("/notice/" + id).then(res => {
          if (res.data.code === 200) {
            this.$message.success("删除成功")
            this.load()
          } else {
            this.$message.error("删除失败")
          }
        })
      },
      handleSelectionChange(val) {
        console.log(val)
        this.multipleSelection = val
      },
      delBatch() {
        let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
        this.request.post("/notice/del/batch", ids).then(res => {
          if (res.data.code === 200) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      },
      reset() {
        this.title = ""
        this.content = ""
        this.load()
      },
      handleSizeChange(pageSize) {
        console.log(pageSize)
        this.pageSize = pageSize
        this.load()
      },
      handleCurrentChange(pageNum) {
        console.log(pageNum)
        this.pageNum = pageNum
        this.load()
      },

      beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 2MB!');
      }
      return isLt2M;
    },
    handleSuccess(response) {
      this.$message.success('上传成功');
      console.log("存储路径："+response.virtualPath);
      console.log("文件名："+response.fileName)
      this.attachmentUrl += response.fileName;
      this.fileName= ''
    }
    }
  }
  </script>
  
  
  <style>
  .headerBg {
    background: #eee!important;
  }
  </style>